{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Ogr创建Geojson\n",
    "- [Python GDAL/OGR Cookbook](https://pcjericks.github.io/py-gdalogr-cookbook/index.html)\n",
    "- [Python与开源GIS](https://www.osgeo.cn/pygis/ogr-ogrwrite.html#id2)\n",
    "- point --> line --> polygon"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "from osgeo import ogr"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## point"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{ \"type\": \"Point\", \"coordinates\": [ 114.323280703, 30.58035027, 0.0 ] }\n"
     ]
    }
   ],
   "source": [
    "# Create a Point\n",
    "point = ogr.Geometry(ogr.wkbPoint) #创建点对象\n",
    "point.AddPoint(114.323280703,30.58035027) #添加点的坐标\n",
    "point_geojson = point.ExportToJson() #将点转为json\n",
    "print(point_geojson) #输出预览json\n",
    "\n",
    "json_path = r'data\\point.json' #定义输出 json的路径与文件名\n",
    "with open(json_path,'w') as f: #以写入的方式打开待定入json数据的json_path文件\n",
    "    f.write(point_geojson) #写入json数据\n",
    "    f.close() #关闭写入"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## MultiPoint"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{ \"type\": \"MultiPoint\", \"coordinates\": [ [ 114.328070282935997, 30.580350273158398, 0.0 ], [ 114.328032732009007, 30.580135521213499, 0.0 ], [ 114.32819366455, 30.580098574594398, 0.0 ] ] }\n"
     ]
    }
   ],
   "source": [
    "multipoint = ogr.Geometry(ogr.wkbMultiPoint) #定义多点对象\n",
    "\n",
    "# 单点1\n",
    "point1 = ogr.Geometry(ogr.wkbPoint)\n",
    "point1.AddPoint(114.328070282936,30.5803502731584)\n",
    "multipoint.AddGeometry(point1) #单点1添加到多点对象中\n",
    "\n",
    "# 单点2\n",
    "point2 = ogr.Geometry(ogr.wkbPoint)\n",
    "point2.AddPoint(114.328032732009,30.5801355212135)\n",
    "multipoint.AddGeometry(point2)\n",
    "\n",
    "# 单点3\n",
    "point3 = ogr.Geometry(ogr.wkbPoint)\n",
    "point3.AddPoint(114.32819366455,30.5800985745944)\n",
    "multipoint.AddGeometry(point3)\n",
    "\n",
    "#转geojson\n",
    "multipoint_geojson = multipoint.ExportToJson()\n",
    "print(multipoint_geojson)\n",
    "\n",
    "json_path = r'data\\multipoint.json' #定义输出 json的路径与文件名\n",
    "with open(json_path,'w') as f: #以写入的方式打开待定入json数据的json_path文件\n",
    "    f.write(multipoint_geojson) #写入json数据\n",
    "    f.close() #关闭写入"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## line 【point--> line】"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{ \"type\": \"LineString\", \"coordinates\": [ [ 114.322915077209473, 30.578888565037637, 0.0 ], [ 114.327163696289062, 30.573715834861623, 0.0 ], [ 114.334716796875, 30.577373722628359, 0.0 ], [ 114.329910278320312, 30.585058532281685, 0.0 ] ] }\n"
     ]
    }
   ],
   "source": [
    "# Create a LineString\n",
    "line = ogr.Geometry(ogr.wkbLineString) #线对象\n",
    "line.AddPoint(114.32291507720947,30.578888565037637) #添加点1\n",
    "line.AddPoint(114.32716369628906,30.573715834861623) #添加点2\n",
    "line.AddPoint(114.334716796875,30.57737372262836) #添加点3\n",
    "line.AddPoint(114.32991027832031,30.585058532281685) #添加点4\n",
    "# print(line.ExportToWkt())\n",
    "\n",
    "line_geojson = line.ExportToJson() #转geojson\n",
    "print(line_geojson)\n",
    "\n",
    "json_path = r'data\\line.json' #定义输出 json的路径与文件名\n",
    "with open(json_path,'w',encoding='GBK') as f:\n",
    "    f.write(line_geojson)\n",
    "    f.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## MultiLine 【line-->MultiLine】"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{ \"type\": \"MultiLineString\", \"coordinates\": [ [ [ 114.328232556581, 30.580858286618401, 0.0 ], [ 114.328199028968001, 30.580765920732802, 0.0 ], [ 114.328195005655004, 30.580650463251999, 0.0 ], [ 114.328221827744997, 30.580568488357098, 0.0 ], [ 114.328278154133997, 30.580492286279998, 0.0 ] ], [ [ 114.327886551617993, 30.580810949113001, 0.0 ], [ 114.328429698944007, 30.580883687221601, 0.0 ] ], [ [ 114.328247308731079, 30.580860595764502, 0.0 ], [ 114.328170865774155, 30.581191957647338, 0.0 ] ] ] }\n"
     ]
    }
   ],
   "source": [
    "multiline = ogr.Geometry(ogr.wkbMultiLineString) #多线对象\n",
    "\n",
    "line1 = ogr.Geometry(ogr.wkbLineString) #线1\n",
    "line1.AddPoint(114.328232556581,30.5808582866184)\n",
    "line1.AddPoint(114.328199028968,30.5807659207328)\n",
    "line1.AddPoint(114.328195005655,30.580650463252)\n",
    "line1.AddPoint(114.328221827745,30.5805684883571)\n",
    "line1.AddPoint(114.328278154134,30.58049228628)\n",
    "multiline.AddGeometry(line1)\n",
    "\n",
    "line2 = ogr.Geometry(ogr.wkbLineString) #线2\n",
    "line2.AddPoint(114.327886551618, 30.580810949113)\n",
    "line2.AddPoint(114.328429698944, 30.5808836872216)\n",
    "multiline.AddGeometry(line2)\n",
    "\n",
    "line3 = ogr.Geometry(ogr.wkbLineString) #线3\n",
    "line3.AddPoint(114.32824730873108,30.580860595764502)\n",
    "line3.AddPoint(114.32817086577415,30.581191957647338)\n",
    "multiline.AddGeometry(line3)\n",
    "\n",
    "multiline_geojson = multiline.ExportToJson() #转geojson\n",
    "print(multiline_geojson)\n",
    "\n",
    "json_path = r'data\\multiline.json' #定义输出 json的路径与文件名\n",
    "with open(json_path,'w',encoding='GBK') as f:\n",
    "    f.write(multiline_geojson)\n",
    "    f.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## polygon 【point-->line-->polygon】"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{ \"type\": \"Polygon\", \"coordinates\": [ [ [ 114.322915077209473, 30.578888565037637, 0.0 ], [ 114.327163696289062, 30.573715834861623, 0.0 ], [ 114.334716796875, 30.577373722628359, 0.0 ], [ 114.329910278320312, 30.585058532281685, 0.0 ], [ 114.322915077209473, 30.578888565037637, 0.0 ] ] ] }\n"
     ]
    }
   ],
   "source": [
    "# Create a Polygon\n",
    "# Create ring\n",
    "ring = ogr.Geometry(ogr.wkbLinearRing)\n",
    "ring.AddPoint(114.32291507720947,30.578888565037637)\n",
    "ring.AddPoint(114.32716369628906,30.573715834861623)\n",
    "ring.AddPoint(114.334716796875,30.57737372262836)\n",
    "ring.AddPoint(114.32991027832031,30.585058532281685)\n",
    "ring.AddPoint(114.32291507720947,30.578888565037637)\n",
    "# Create polygon\n",
    "poly = ogr.Geometry(ogr.wkbPolygon)\n",
    "poly.AddGeometry(ring)\n",
    "\n",
    "polygon_geojson = poly.ExportToJson()\n",
    "print(polygon_geojson)\n",
    "\n",
    "json_path = r'data\\polygon.json' #定义输出 json的路径与文件名\n",
    "with open(json_path,'w',encoding='GBK') as f:\n",
    "    f.write(polygon_geojson)\n",
    "    f.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## MultiPolygon 【point-->line-->polygon-->MultiPolygon】"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{ \"type\": \"MultiPolygon\", \"coordinates\": [ [ [ [ 114.328590631484985, 30.580943724984383, 0.0 ], [ 114.32971715927124, 30.580897542093247, 0.0 ], [ 114.329172670841217, 30.58141248008457, 0.0 ], [ 114.328590631484985, 30.580943724984383, 0.0 ] ] ], [ [ [ 114.328311681747437, 30.58134320607455, 0.0 ], [ 114.328400194644928, 30.581250840650867, 0.0 ], [ 114.328939318656921, 30.581710357763651, 0.0 ], [ 114.328823983669281, 30.581802722749583, 0.0 ], [ 114.328311681747437, 30.58134320607455, 0.0 ] ] ], [ [ [ 114.328276813030229, 30.580521150710283, 0.0 ], [ 114.328547716140747, 30.580752065842397, 0.0 ], [ 114.328427016735077, 30.580874450639435, 0.0 ], [ 114.328236579895005, 30.580858286618497, 0.0 ], [ 114.328209757804871, 30.58079132136017, 0.0 ], [ 114.328201711177826, 30.580687409660836, 0.0 ], [ 114.328223168849945, 30.580592734460055, 0.0 ], [ 114.328276813030229, 30.580521150710283, 0.0 ] ] ] ] }\n"
     ]
    }
   ],
   "source": [
    "multipolygon = ogr.Geometry(ogr.wkbMultiPolygon)\n",
    "\n",
    "# Create ring #1\n",
    "ring1 = ogr.Geometry(ogr.wkbLinearRing)\n",
    "ring1.AddPoint(114.32859063148499,30.580943724984383)\n",
    "ring1.AddPoint(114.32971715927124,30.580897542093247)\n",
    "ring1.AddPoint(114.32917267084122,30.58141248008457)\n",
    "ring1.AddPoint(114.32859063148499,30.580943724984383)\n",
    "\n",
    "# Create polygon #1\n",
    "poly1 = ogr.Geometry(ogr.wkbPolygon)\n",
    "poly1.AddGeometry(ring1)\n",
    "multipolygon.AddGeometry(poly1)\n",
    "\n",
    "# Create ring #2\n",
    "ring2 = ogr.Geometry(ogr.wkbLinearRing)\n",
    "ring2.AddPoint(114.32831168174744,30.58134320607455)\n",
    "ring2.AddPoint(114.32840019464493,30.581250840650867)\n",
    "ring2.AddPoint(114.32893931865692,30.58171035776365)\n",
    "ring2.AddPoint(114.32882398366928,30.581802722749583)\n",
    "ring2.AddPoint(114.32831168174744,30.58134320607455)\n",
    "\n",
    "# Create polygon #2\n",
    "poly2 = ogr.Geometry(ogr.wkbPolygon)\n",
    "poly2.AddGeometry(ring2)\n",
    "multipolygon.AddGeometry(poly2)\n",
    "\n",
    "# Create ring #3\n",
    "ring3 = ogr.Geometry(ogr.wkbLinearRing)\n",
    "ring3.AddPoint(114.32827681303023,30.580521150710283)\n",
    "ring3.AddPoint(114.32854771614075,30.580752065842397)\n",
    "ring3.AddPoint(114.32842701673508,30.580874450639435)\n",
    "ring3.AddPoint(114.328236579895,30.580858286618497)\n",
    "ring3.AddPoint(114.32820975780487,30.58079132136017)\n",
    "ring3.AddPoint(114.32820171117783,30.580687409660836)\n",
    "ring3.AddPoint(114.32822316884995,30.580592734460055)\n",
    "ring3.AddPoint(114.32827681303023,30.580521150710283)\n",
    "\n",
    "# Create polygon #3\n",
    "poly3 = ogr.Geometry(ogr.wkbPolygon)\n",
    "poly3.AddGeometry(ring3)\n",
    "multipolygon.AddGeometry(poly3)\n",
    "\n",
    "polygon_multi_geojson = multipolygon.ExportToJson()\n",
    "print(polygon_multi_geojson)\n",
    "\n",
    "json_path = r'data\\multipolygon.json' #定义输出 json的路径与文件名\n",
    "with open(json_path,'w',encoding='GBK') as f:\n",
    "    f.write(polygon_multi_geojson)\n",
    "    f.close()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
